/**
 * fileName: Accordion.java
 *     Date: 2012-6-16
 **/
package com.stranger.common.tag.accordion;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;



/**
 * @作者 stranger.lin
 *
 * @时间 2012-6-16
 */
public class AccordionsTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String id;
	private List<Accordion> accordions;
	private 	JspWriter out;
	
	@Override
	public int doStartTag() throws JspException {
		try {
			if (accordions==null) {
				accordions=new ArrayList<Accordion>();
			}
			
			out=pageContext.getOut();
			if (id==null||"".equals(id.trim())) {
				id="accordions_"+UUID.randomUUID();
			}
			StringBuffer html=new StringBuffer("<div id=\""+id+"\">");
			out.print(html);
			out.flush();
			
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		
		return EVAL_BODY_INCLUDE;
	}

	
	@Override
	public int doEndTag() throws JspException {
		try {
			if (out==null) {
				out=pageContext.getOut();
			}
			StringBuffer html=new StringBuffer("</div>");
			html.append("<script>\n");
			html.append("$(function() {\n");
			html.append("$( \"#"+id+"\" ).accordion();\n});\n");
			html.append("</script>\n");
			out.print(html);
			out.flush();
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		id=null;
		accordions=null;
		return EVAL_PAGE;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}


	public List<Accordion> getAccordions() {
		return accordions;
	}


	public void setAccordions(List<Accordion> accordions) {
		this.accordions = accordions;
	}



	
	
	

}
